package com.guofen.controller.user;

import com.guofen.pojo.User;
import com.guofen.service.user.UserService;
import com.guofen.util.result.Result;
import com.guofen.util.result.ResultGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/user")
@Controller
@Api(tags = "UserController", description = "用户表")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("登录")
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    public Result login(@ApiParam(name="name",value="用户名",required=true) @RequestParam String name,
                        @ApiParam(name="password",value="密码",required=true) @RequestParam String password) {
        return ResultGenerator.genSuccessResult(userService.login(name, password));
    }

    @ApiOperation("添加用户")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public Result add(@ApiParam(name="name",value="用户名",required=true) @RequestParam String name,
                      @ApiParam(name="password",value="密码",required=true) @RequestParam String password,
                      @ApiParam(name="position",value="角色",required=true) @RequestParam String position,
                      @ApiParam(name="permission_level",value="角色等级",required=true) @RequestParam String permission_level,
                      @ApiParam(name="type",value="角色类型",required=true) @RequestParam String type) {
        User user = new User();
        user.setName(name);
        user.setPassword(password);
        user.setPosition(position);
        user.setPermission_level(permission_level.replaceAll(" ", ""));
        user.setType(type);
        return ResultGenerator.genSuccessResult(userService.add(user));
    }

    @ApiOperation("删除用户")
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@ApiParam(name="id",value="ID",required=true) @RequestParam Integer id) {
        return ResultGenerator.genSuccessResult(userService.delete(id));
    }

    @ApiOperation("修改用户")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public Result update(@ApiParam(name="id",value="ID",required=true) @RequestParam Integer id,
                         @ApiParam(name="name",value="用户名",required=true) @RequestParam String name,
                         @ApiParam(name="password",value="密码",required=true) @RequestParam String password,
                         @ApiParam(name="position",value="角色",required=true) @RequestParam String position,
                         @ApiParam(name="permission_level",value="角色等级",required=true) @RequestParam String permission_level,
                         @ApiParam(name="type",value="角色类型",required=true) @RequestParam String type) {
        User user = new User();
        user.setId(id);
        user.setName(name);
        user.setPassword(password);
        user.setPosition(position);
        user.setPermission_level(permission_level.replaceAll(" ", ""));
        user.setType(type);
        return ResultGenerator.genSuccessResult(userService.update(user));
    }

    @ApiOperation("查找用户")
    @RequestMapping(value = "/select",method = RequestMethod.POST)
    @ResponseBody
    public Result select(@ApiParam(name="name",value="用户名") @RequestParam(defaultValue = "") String name,
                         @ApiParam(name="pageSize",value="页容量",required=true) @RequestParam Integer pageSize,
                         @ApiParam(name="currentPage",value="当前页",required=true) @RequestParam Integer currentPage) {
        return ResultGenerator.genSuccessResult(userService.get(name, pageSize, currentPage));
    }
}
